﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SangoBang
{
    using Cards;
    using Cards.Characters;
    class GameController
    {
        public void addPlayer(Player player) {
            playerList.Add(player);
        }
        private RoleCard[] roleCardFull = new RoleCard[]{
            new RoleCard("P主公"),
            new RoleCard("P忠臣"),
            new RoleCard("N反贼"),
            new RoleCard("A内奸"),
            new RoleCard("N反贼"),
            new RoleCard("N反贼"),
            new RoleCard("P忠臣"),
            new RoleCard("N反贼"),
            new RoleCard("P忠臣"),
            new RoleCard("A内奸"),
        };
        private CharacterCard[] characterCardFull = new CharacterCard[]{
            new CharacterCard(CharacterEnum.Caocao),
            new CharacterCard(CharacterEnum.Liubei),
            new CharacterCard(CharacterEnum.Lvbu),
            new CharacterCard(CharacterEnum.Sunquan),
            new CharacterCard(CharacterEnum.Taishici),
            new CharacterCard(CharacterEnum.Xiahoudun),
            new CharacterCard(CharacterEnum.Xuzhu),
            new CharacterCard(CharacterEnum.Zhangfei),
            new CharacterCard(CharacterEnum.Zhouyu),
            new CharacterCard(CharacterEnum.Zhugeliang)
        };

        public void dealRole() 
        {
            int playerNum = playerList.Count;
            RoleCard[] roleCards = new RoleCard[playerNum];
            int i = 0;
            for (i = 0; i < playerNum; ++i) {
                roleCards[i] = roleCardFull[i];
            }
            shuffle(roleCards);
            for (i = 0; i < playerNum; ++i) {
                playerList[i].mRole = roleCards[i].ToString();
            }
        }
        public void dealCharacter()
        {
            int playerNum = playerList.Count;
            int characterNum = characterCardFull.Length;
            CharacterCard[] characterCards = new CharacterCard[characterNum];
            int i = 0;
            for (i = 0; i < characterNum; ++i)
            {
                characterCards[i] = characterCardFull[i];
            }
            shuffle(characterCards);
            for (i = 0; i < playerNum; ++i)
            {
                playerList[i].mCharacterName = characterCards[i].mCharacterEnum.ToString();
                playerList[i].mCharacter = Character.sCharacterDict[characterCards[i].mCharacterEnum];
            }
        }

        public void ResetResource()
        {
            dealRole();
            dealCharacter();
        }

        private void shuffle(RoleCard[] cards)
        {
            int size = cards.Length;
            Random rand = new Random();
            int i = size - 1;
            for (; i > 0; i--) {
                int j = rand.Next(i);
                RoleCard temp = cards[i];
                cards[i] = cards[j];
                cards[j] = temp;
            }
        }
        private void shuffle(CharacterCard[] cards)
        {
            int size = cards.Length;
            Random rand = new Random();
            int i = size - 1;
            for (; i > 0; i--)
            {
                int j = rand.Next(i);
                CharacterCard temp = cards[i];
                cards[i] = cards[j];
                cards[j] = temp;
            }
        }
        private List<Player> playerList = new List<Player>();

    }
}
